﻿// Copyright 2009 William P. Hickey Jr. and William U. Walker
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//     
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AgwpePort;
using AgwpePort.Aprs;

namespace CSReporter
{
    public class MessageRules
    {
        public static void Process(Radio radio, MessageRuleList rules, AgwpeMoniUnproto data, DateTime timeHeard)
        {
            List<RuleHandler> handlers = new List<RuleHandler>();

            var ruleList =
                from row in rules
                orderby row.Order
                select row;

            foreach(MessageRule row in ruleList.ToList())
            {
                switch(row.RuleType)
                {
                    case 0: // Forward to other packet station.
                        handlers.Add(new Forward());
                        break;
                    case 1: // Forward to Email address.
                        handlers.Add(new MsgToEmail());
                        break;
                }
                if (handlers.Count > 1)
                {
                    handlers[handlers.Count - 2].NextRule(handlers[handlers.Count - 1]);
                }
            }
        }
    }

    /// <summary>
    /// Abstract rule handler base class definition.
    /// </summary>
    abstract class RuleHandler
    {
        protected RuleHandler nextRule;

        public void NextRule(RuleHandler nextRule)
        {
            this.nextRule = nextRule;
        }

        public abstract void RunMsgRule(MessageRule rule, AgwpeMoniUnproto data, DateTime timeHeard);
    }

    /// <summary>
    /// Rule handler for forwarding a message to a different packet radio station.
    /// </summary>
    class Forward : RuleHandler
    {
        public override void RunMsgRule(MessageRule rule, AgwpeMoniUnproto data, DateTime timeHeard)
        {
            // Forward the message to the requested packet radio station.

            // Then process the next rule.
            if (nextRule != null)
                nextRule.RunMsgRule(rule, data, timeHeard);
        }
    }

    /// <summary>
    /// Rule handler for forwarding a message to an internet email address.
    /// </summary>
    class MsgToEmail : RuleHandler
    {
        public override void RunMsgRule(MessageRule rule, AgwpeMoniUnproto data, DateTime timeHeard)
        {
            // Send the message to the requested internet email adderess.

            // Then process the next rule.
            if (nextRule != null)
                nextRule.RunMsgRule(rule, data, timeHeard);
        }
    }
}
